package cn.kgc.springbootdemo.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.kgc.springbootdemo.util.R;

import cn.kgc.springbootdemo.entity.Menu;
import cn.kgc.springbootdemo.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 菜单表(Menu)表控制层
 *
 * @author makejava
 * @since 2021-03-31 17:10:40
 */
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
@Validated
@Slf4j
@Api(value = "MenuController",tags = "菜单管理控制器")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;



    /**
     * 新增数据
     *
     * @param menu 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增菜单")
    public R insert(@RequestBody Menu menu) {
        return  R.ok(this.menuService.save(menu));
    }

    /**
     * 修改数据
     *
     * @param menu 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation("修改菜单")
    public R update(@RequestBody Menu menu) {
        return  R.ok(this.menuService.updateById(menu));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除菜单")
    public R delete(@Param ("主键")@PathVariable String id) {
        return  R.ok(this.menuService.removeById(id));
    }

    @GetMapping("/tree")
    @ApiOperation("书查询菜单")
    public R<List<Tree<String>>> getTree(){
        return R.ok(menuService.getTree());
    }
}
